package com.happyclub.paul.service.impl;

import com.happyclub.paul.dao.AlbumPhotoMapRepostitory;
import com.happyclub.paul.dao.ManualAuditRepostitory;
import com.happyclub.paul.dao.PhotoLibraryRepostitory;
import com.happyclub.paul.dao.UserRepostitory;
import com.happyclub.paul.service.IPhotoLibraryService;
import com.happyclub.pojo.TbAlbumPhotoMap;
import com.happyclub.pojo.TbPhotoLibrary;
import com.happyclub.pojo.TbUser;
import com.happyclub.utils.JsonResult;
import com.happyclub.utils.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 魏泉琦
 * @Date: 2019/4/7 12:10
 * @Description: 相片表
 */
@Service
public class PhotoLibraryServiceImpl implements IPhotoLibraryService {

    @Autowired
    private PhotoLibraryRepostitory photoLibraryRepostitory;

    @Autowired
    private UserRepostitory userRepostitory;

    @Autowired
    private AlbumPhotoMapRepostitory albumPhotoMapRepostitory;
    
    
    @Autowired
    private ManualAuditRepostitory manualAuditRepostitory;

    /**
     *@Description: 通过相片编号查询相片信息
     *@Author: 魏泉琦
     *@date: 2019/4/7
     */
    @Override
    public TbPhotoLibrary queryPhotoLibraryByPhotoId(String photoId) {
        TbPhotoLibrary photoLibrary = photoLibraryRepostitory.queryPhotoLibraryByPhotoId(photoId);
        return photoLibrary;
    }


    /**
     *@Description: 相片库增加相片
     *@Author: 魏泉琦
     *@date: 2019/4/8
     */
    @Transactional
    @Override
    public Integer createPhotoLibrary(String id,String photoUrl,String uerId,String albumId,Integer isOk,String activityId){
        Integer cnt = photoLibraryRepostitory.createPhotoLibrary(id, photoUrl, uerId, albumId,isOk,activityId);
        return cnt;
    }


    /**
     *@Description: 查询所有相片库中相片状态的相片 -1未通过 0待审核 1已通过
     *@Author: 魏泉琦
     *@date: 2019/4/11
     */
    @Override
    public JsonResult queryTbPhotoLibrariesByOk(long isOk){
        List<TbPhotoLibrary> photoLibraries = new ArrayList<>();
        JsonResult jsonResult = new JsonResult();
        if(isOk==-1){
            photoLibraries = photoLibraryRepostitory.findTbPhotoLibrariesByOk(isOk);
        }
        if(isOk==0){
            photoLibraries = photoLibraryRepostitory.findTbPhotoLibrariesByOk(isOk);
        }
        if(isOk==1) {
            photoLibraries = photoLibraryRepostitory.findTbPhotoLibrariesByOk(isOk);
        }
        if(null==photoLibraries || photoLibraries.size()==0){   //没有数据
            jsonResult.setMsg("未有数据");
            jsonResult.setErrorCode("NO");
            return jsonResult;
        }

        List<TbPhotoLibrary> tbPhotoLibraries = new ArrayList<>();

        //查询出每张相片所属用户
        for(TbPhotoLibrary photoLibrarie:photoLibraries){
            String userId = photoLibrarie.getUserId();
            TbUser user = userRepostitory.queryUserById(userId);
            photoLibrarie.setUser(user);
            tbPhotoLibraries.add(photoLibrarie);
        }

        jsonResult.setErrorCode("OK");
        jsonResult.setResult(tbPhotoLibraries);
        jsonResult.setMsg("有货");
        return jsonResult;
    }

    /**
     *@Description: 保存并修改审核通过的相片
     *@Author: 魏泉琦
     *@date: 2019/4/11
     */
    @Transactional
    @Override
    public JsonResult savePassPhoto(String photoId, String albumId,Integer isPass,TbUser user) {
        JsonResult jsonResult =new JsonResult();
        Integer cnt = photoLibraryRepostitory.updatePhotoByPhotoId(isPass,photoId);      //isPass 审核状态   -1未通过 0待审核 1通过
        if(cnt<=0){
            jsonResult.setErrorCode("NO");
            jsonResult.setMsg("网络波动");
            return jsonResult;
        }

       //既然相片状态修改成功，那么要清楚那个人审核的，审核是否通过，什么时间审核
        manualAuditRepostitory.createManualAudit(Uuid.getUUID(), photoId, user.getUserId(), isPass, user.getNickName());


        //相片表相片状态修改成功

        //如果将相片的状态修改为 未通过，那么我们要修改相册相片表的数据，有则删除
        if(isPass==-1){
            //查询相册相片表中 相片编号是否存在
            TbAlbumPhotoMap albumPhotoMap = albumPhotoMapRepostitory.queryTbAlbumPhotoMapByPhotoId(photoId);
            if(albumPhotoMap!=null){        //相册相片表中相片存在,那么删除
                albumPhotoMapRepostitory.deleteTbAlbumPhotoMapByPhotoId(photoId);
            }
        }
        //如果此时的相片状态时审核通过，那么更新相册相片表数据
        if(isPass==1){
            //保存相册相片表信息
            Integer ct = albumPhotoMapRepostitory.createPhotoAlbumMapByAlbumIdAndPhotoId(albumId, photoId);
            if(ct<=0){       //相册相片表数据插入失败
                jsonResult.setErrorCode("NO");
                jsonResult.setMsg("网络波动");
                return jsonResult;
            }
        }
        //成功
        jsonResult.setErrorCode("OK");
        jsonResult.setMsg("更新成功");


        return jsonResult;
    }
}
